import Vue from 'vue';
import VueRouter from 'vue-router';
import Login from '@/view/login/Login.vue'; // 引入 Home页面组件
import PATH from '@/js/util/path.js';
import user from "@/js/util/user.js";
import message from '@/js/util/message.js';
import Main from '@/view/main/index.vue';

// 注册路由插件
Vue.use(VueRouter);

// 解决路由跳转到本身报错
let originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

let routes = [
  {
    path: '/',
    name: 'Main',
    component: Main,
    redirect: PATH.SCENE,
    children: [
        {
            path: PATH.SCENE,
            name: 'Scene',
            meta: {
                key: 'SCENE',
                title: '主页'
            },
            component: () => import('@/view/main/Scene.vue')
        },
        {
            path: PATH.SHARE,
            name: 'Share',
            meta: {
                key: 'SHARE',
                title: '我的分享'
            },
            component: () => import('@/view/main/Share.vue')
        },
        {
            path: PATH.RECYCLE,
            name: 'Recycle',
            meta: {
                key: 'RECYCLE',
                title: '回收站'
            },
            component: () => import('@/view/main/Recycle.vue')
        },
        {
            path: PATH.TEMPLETE,
            name: 'Templete',
            meta: {
                key: 'TEMPLETE',
                title: '模板'
            },
            component: () => import('@/view/main/Templete.vue')
        }
    ]
  },
  {
    path: PATH.INDEX,
    name: 'Index',
    component: Login
  },
  {
    path: PATH.HOME,
    name: 'Home',
    component: () => import('@/view/main/Home.vue')
  },
//   {
//     path: PATH.LOGINLIC,
//     name: 'LoginLic',
//     component: () => import('@/view/login/LoginLic.vue')
//   },
//   {
//     path: PATH.LOGIN,
//     name: 'Login',
//     component: Login
//   },
//   {
//     path: PATH.REGISTER,
//     name: 'Register',
//     component: () => import('@/view/login/Register.vue')
//   },
//   {
//     path: PATH.README,
//     name: 'Readme',
//     component: () => import('@/view/login/Readme.vue')
//   },
//   {
//     path: PATH.PASSWORD,
//     name: 'Password',
//     component: () => import('@/view/login/Password.vue')
//   },
  {
    path: PATH.VIEWER,
    name: 'Viewer',
    component: () => import('@/view/main/Viewer.vue')
  },
//   {
//     path: PATH.SINGLELOGIN,
//     name: 'SingleLogin',
//     component: () => import('@/view/login/SingleLogin.vue')
//   },
  {
    path: PATH.LOGINEXPLORER,
    name: 'LoginExplorer',
    component: () => import('@/view/login/LoginExplorer.vue')
  }
];

let router = new VueRouter({
  routes
});

let whiteList = [PATH.VIEWER, PATH.LOGINEXPLORER, PATH.SCENE];

router.beforeEach((to, from, next) => {
    let token = user.getToken();
    // 如果未匹配到路由
    if (to.matched.length === 0) {
        message.message('无效路由！', 'error');
        next();
    } else {
        if (token !== '' && token !== null && token !== 'undefined' && token !== undefined) {
            next();
        } else {
            if (whiteList.indexOf(to.path) !== -1) {
                next();
            } else {
                next(PATH.SCENE);
            }
        }
    }
});

function getUrlParam(name) {
    let url = window.location.href;
    let query = url.split("?")[1];
    if (query) {
        let vars = query.split("&");
        for (let i=0;i<vars.length;i++) {
            let pair = vars[i].split("=");
            if(pair[0] == name) return pair[1];
        }
    }
    return(false);
}

export default router;